//
//  ExclusiveTableViewCell.m
//  LuckyRadio
//
//  Created by Mr、J on 15/11/18.
//  Copyright © 2015年 chenjun. All rights reserved.
//

#import "ExclusiveTableViewCell.h"
#import "CustomerNetworking.h"
#import <UIImageView+WebCache.h>
#import "AsiaNewSongModel.h"
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width

#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#define WIDTH_PRO(X) (X)*(SCREEN_WIDTH/375)

#define HEIGHT_PRO(Y) (Y)*(SCREEN_HEIGHT/667)
@implementation ExclusiveTableViewCell
- (void)dealloc
{
    [_tagImage release];
    [_namelabel release];
    [_moreButton release];
    [_titleLabel release];
    [_songsPageModel release];
    [_songsArray release];
    [super dealloc];
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self createSubViews];
        
        [self loadData];

    }
    return self;
}
-(void)loadData
{
    self.songsArray = [NSMutableArray array];
    [CustomerNetworking customerWithURL:@"http://api.dongting.com/frontpage/frontpage?location=0&version=1447252806&app=ttpod&v=v8.1.1.2015110616&user_id=0&mid=iPhone5S&f=f234&s=s310&imsi=&hid=&splus=8.4.1&active=1&net=2&openudid=e97c1f17f916efa173065a10152c7506e65b60f7&idfa=5FBD2BF5-1484-4C9B-A138-4D9CE22EADB5&utdid=VkKezJUW5SwDAGga0yzrZnyO&alf=700606&bundle_id=com.ttpod.music&latitude=38.89679373280231&longtitude=121.5286730881665" andParamer:nil andBlock:^(id response) {
        
        NSArray *listArray = [NSArray arrayWithArray: [[[response objectForKey:@"data"] objectAtIndex:13] objectForKey:@"data"]];
        for (NSDictionary *dic in listArray) {
            AsiaNewSongModel *asiaNewSongModel = [AsiaNewSongModel modelWithDictionary:dic];
            [self.songsArray addObject:asiaNewSongModel];
        }
        
        for (int i = 0; i < _songsArray.count; i++) {
            AsiaNewSongModel *asiaNewSongModel = _songsArray[i];
            UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH_PRO(20) + WIDTH_PRO(120) * i, HEIGHT_PRO(40), WIDTH_PRO(80), HEIGHT_PRO(80))];
            image.layer.cornerRadius = WIDTH_PRO(40);
            image.clipsToBounds = YES;
            image.backgroundColor = [UIColor blueColor];
            [self.contentView addSubview:image];
            [image sd_setImageWithURL:[NSURL URLWithString:asiaNewSongModel.picUrl]];
            
            UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH_PRO(30) + WIDTH_PRO(120) * i, HEIGHT_PRO(130), WIDTH_PRO(80), HEIGHT_PRO(20))];
            textLabel.font = [UIFont systemFontOfSize:15];
            [self.contentView addSubview:textLabel];
            textLabel.text = asiaNewSongModel.name;
            [textLabel release];
            
        }
        
        
    }];
}
-(void)createSubViews
{
    
    self.namelabel = [UILabel new];
    self.namelabel.font = [UIFont systemFontOfSize:15];
    [self.contentView addSubview:self.namelabel];
    [_namelabel release];
    
    
    self.titleLabel = [[UILabel alloc] init];
    [self.contentView addSubview:self.titleLabel];
    [_titleLabel release];
    
    self.moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.moreButton addTarget:self action:@selector(moreSongs:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.moreButton];
    
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    self.titleLabel.frame = CGRectMake(WIDTH_PRO(10), HEIGHT_PRO(10), SCREEN_WIDTH, HEIGHT_PRO(20));
    
    self.moreButton.frame = CGRectMake(SCREEN_WIDTH - WIDTH_PRO(60), HEIGHT_PRO(10), WIDTH_PRO(50), HEIGHT_PRO(20));
    
}
-(void)setSongsPageModel:(SongsPageModel *)songsPageModel
{
    if (_songsPageModel != songsPageModel) {
        [_songsPageModel release];
        _songsPageModel = [songsPageModel retain];
    }
    
    self.titleLabel.text = songsPageModel.name;
    [self.moreButton setTitle:@"更多 >" forState:UIControlStateNormal];
    self.moreButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.moreButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
}

+(ExclusiveTableViewCell *)exclusiveTableViewCellWith:(UITableView *)tableView{
    static NSString *cellIdentify = @"exclusiveCell";
    ExclusiveTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
    if (!cell) {
        cell = [[ExclusiveTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify];
    }
    return cell;
    
}

-(void)moreSongs:(id)sender
{
    [self.delegate pushToMoreExclusive];
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
